package com.example.queryexpressdelivery_01.activity;

import android.os.Bundle;

import androidx.activity.EdgeToEdge;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.example.queryexpressdelivery_01.fragment.BillQueryCompanyFragment;
import com.example.queryexpressdelivery_01.fragment.CompanyQueryFragment;
import com.example.queryexpressdelivery_01.adapter.FragmentAdapter;
import com.example.queryexpressdelivery_01.fragment.IndexFragment;
import com.example.queryexpressdelivery_01.fragment.OtherFragment;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.fragment.TraceFragment;
import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;

import java.util.ArrayList;

public class UserActivity extends AppCompatActivity {
    private ArrayList<Fragment> fragments;
    private FragmentAdapter fragmentAdapter;
    private ViewPager2 vp2UserFragment;
    private TabLayout tlUserTab;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_user);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        int id = getIntent().getIntExtra("id",-1);
        fragments = new ArrayList<>();
        fragments.add(new IndexFragment("首页",String.valueOf(id)));
        fragments.add(new TraceFragment("跟踪",String.valueOf(id)));
        fragments.add(new CompanyQueryFragment("查公司全称",String.valueOf(id)));
        fragments.add(new BillQueryCompanyFragment("单号查公司"));
        fragments.add(new OtherFragment("其它",String.valueOf(id)));
        fragmentAdapter = new FragmentAdapter(getSupportFragmentManager(),getLifecycle());
        fragmentAdapter.setFragments(fragments);
        vp2UserFragment.setAdapter(fragmentAdapter);

        new TabLayoutMediator(tlUserTab, vp2UserFragment, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(fragments.get(position).getArguments().getString("param1"));
            }
        }).attach();
    }

    public void setViewPageItem(int position){
        vp2UserFragment.setCurrentItem(position);
    }

    private void initView() {
        vp2UserFragment = (ViewPager2) findViewById(R.id.vp2UserFragment);
        tlUserTab = (TabLayout) findViewById(R.id.tlUserTab);
    }
}